import 'dart:io';

import 'package:ddstore/dd_notifier.dart';
import 'package:flutter/material.dart';

typedef DDButtonBuilder = Widget Function(BuildContext context, bool enable, bool selected);

enum DDButtonTapStyle {
  highlight,
  ink,
}

class DDButtonController extends DDNotifier {
  final bool selected;
  final bool enable;
  DDButtonController({
    this.enable = true,
    this.selected = true,
  });
}

class _DDContainerButton extends StatefulWidget {
  final VoidCallback? onTap;
  final VoidCallback? onDoubleTap;
  final VoidCallback? onLongPress;
  final DDButtonController controller;
  final Alignment? alignment;
  final EdgeInsets? padding;
  final double? width;
  final double? height;
  final Widget? child;
  final Color? color;
  final Color? disableColor;
  final Color? selectedColor;
  final BoxDecoration? decoration;
  final BoxDecoration? disableDecoration;
  final BoxDecoration? selectedDecoration;
  final BoxConstraints? constraints;
  final Clip? clipBehavior;
  final DDButtonTapStyle style;
  final bool topInk;
  const _DDContainerButton({
    super.key,
    this.onTap,
    this.onDoubleTap,
    this.onLongPress,
    required this.controller,
    this.alignment,
    this.padding,
    this.width,
    this.height,
    this.child,
    this.color,
    this.disableColor,
    this.selectedColor,
    this.decoration,
    this.disableDecoration,
    this.selectedDecoration,
    this.constraints,
    this.clipBehavior,
    this.style = DDButtonTapStyle.highlight,
    this.topInk = false,
  });

  @override
  State<_DDContainerButton> createState() => __DDContainerButtonState();
}

class __DDContainerButtonState extends State<_DDContainerButton> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var currentColor = widget.controller.enable
        ? (widget.controller.selected ? widget.selectedColor : widget.color)
        : widget.disableColor;
    currentColor ??= widget.color;
    var currentDecoration = widget.controller.enable
        ? (widget.controller.selected ? widget.selectedDecoration : widget.decoration)
        : widget.disableDecoration;
    currentDecoration ??= widget.decoration;
    if (currentDecoration == null) {
      currentDecoration = BoxDecoration(color: currentColor);
    } else {
      currentDecoration = currentDecoration.copyWith(color: currentColor);
    }
    currentColor = null;

    if (!widget.controller.enable) {
      return Container(
        color: currentColor,
        decoration: currentDecoration,
        alignment: widget.alignment,
        padding: widget.padding,
        width: widget.width,
        height: widget.height,
        constraints: widget.constraints,
        clipBehavior: widget.clipBehavior ?? Clip.none,
        child: widget.child,
      );
    }
    if (widget.topInk) {
      return Stack(
        children: [
          Container(
            color: currentColor,
            decoration: currentDecoration,
            alignment: widget.alignment,
            padding: widget.padding,
            width: widget.width,
            height: widget.height,
            constraints: widget.constraints,
            clipBehavior: widget.clipBehavior ?? Clip.none,
            child: widget.child,
          ),
          Positioned.fill(
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                borderRadius: currentDecoration.borderRadius as BorderRadius?,
                radius: (Platform.isIOS || Platform.isMacOS) ? 0.0 : null,
                onTap: widget.onTap,
                onDoubleTap: widget.onDoubleTap,
                onLongPress: widget.onLongPress,
              ),
            ),
          ),
        ],
      );
    }
    return Material(
      color: Colors.transparent,
      child: Ink(
        width: widget.width,
        height: widget.height,
        decoration: currentDecoration,
        // padding: widget.padding,
        child: InkWell(
          borderRadius: currentDecoration.borderRadius as BorderRadius?,
          radius: (Platform.isIOS || Platform.isMacOS) ? 0.0 : null,
          onTap: widget.onTap,
          onDoubleTap: widget.onDoubleTap,
          onLongPress: widget.onLongPress,
          child: Container(
            width: widget.width,
            height: widget.height,
            constraints: widget.constraints,
            clipBehavior: widget.clipBehavior ?? Clip.none,
            alignment: widget.alignment,
            padding: widget.padding,
            child: widget.child,
          ),
        ),
      ),
    );
  }
}
